package com.liu.jbase.controller.impl;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.controller.IBaseController;
import com.liu.jbase.service.IBaseService;
import com.liu.jbase.util.StringEditor;
import com.liu.tools.StrUtil;

public class BaseControllerImpl<R, T, E extends IBaseService<R, T>> implements IBaseController<R, T> {

	@Autowired
	private E baseService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
    	
        binder.registerCustomEditor(String.class, new StringEditor());

        // 日期类处理
        try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateFormat.setLenient(false);
			binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
		} catch (Exception e) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			dateFormat.setLenient(false);
			binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
		}
    }
	
	@ResponseBody
    @RequestMapping("/list")
	@Override
	public ResultVo<List<R>> list(T obj, HttpServletRequest request) throws Exception {
		return baseService.list(obj, request);
	}

	@ResponseBody
    @RequestMapping("/single")
	@Override
	public ResultVo<R> single(String obj, HttpServletRequest request)
			throws Exception {
		return baseService.findById(obj, request);
	}
	
	@ResponseBody
    @RequestMapping("/detail")
	@Override
	public ResultVo<R> detail(T obj, HttpServletRequest request)
			throws Exception {
		return baseService.find(obj, request);
	}

	@ResponseBody
    @RequestMapping("/count")
	@Override
	public ResultVo<R> count(T obj, HttpServletRequest request)
			throws Exception {
		return baseService.count(obj, request);
	}

	@ResponseBody
    @RequestMapping("/update")
	@Override
	public ResultVo<R> update(T obj, HttpServletRequest request)
			throws Exception {
		return baseService.update(obj, request);
	}

	@ResponseBody
    @RequestMapping("/save")
	@Override
	public ResultVo<R> save(T obj, HttpServletRequest request) throws Exception {
		return baseService.add(obj, request);
	}

	@ResponseBody
    @RequestMapping("/page")
	@Override
	public ResultVo<PageListVo<R>> page(T obj, PageListVo<R> pageListVo, HttpServletRequest request) throws Exception {

		return baseService.page(obj, pageListVo, request);
	}

	@ResponseBody
    @RequestMapping("/delete/by/id")
	@Override
	public ResultVo<R> deleteById(Serializable id, HttpServletRequest request) throws Exception {
		return baseService.deleteById(id, request);
	}

	@ResponseBody
    @RequestMapping("/delete")
	@Override
	public ResultVo<R> delete(T obj, HttpServletRequest request) throws Exception {
		return baseService.delete(obj, request);
	}

    @ResponseBody
    @RequestMapping("/page/list")
    @Override
    public PageListVo<R> pagination(T obj, PageListVo<R> pageListVo, HttpServletRequest request) {
		return baseService.page(obj, pageListVo, request).getData();
	}

    @RequestMapping("/view")
	@Override
	public String view(T obj, Model model, HttpServletRequest request) throws Exception {
		
    	String pv = request.getParameter("pv");
    	if(StrUtil.isNotNull(pv)){
    		String[] p = pv.split(":");
    		
    		if("page".equals(p[1])){
    			model.addAttribute("result", baseService.page(obj, new PageListVo<>(), request));
    		}
    		
    		if("single".equals(p[1])){
    			model.addAttribute("result", baseService.findById(obj.toString(), request));
    		}
    		
    		if("detail".equals(p[1]) || "update".equals(p[1]) || "modify".equals(p[1])){
    			model.addAttribute("result", baseService.find(obj, request));
    		}
    		
    		if("list".equals(p[1])){
    			model.addAttribute("result",baseService.list(obj, request));
    		}
    	}
		return pv.replace(":", "/");
	}
	
}
